/***************************************
 * 说明：本文件主要列举第4章课后习题
 * *************************************/
#include "test.h"
#include <string.h>
/******************************************
 * 习题1：定义一个Complex类，重载‘+’运算符，使
 * 其能够用于复数的加法运算，重载为非友元非成员函数
 * ****************************************/
Complex::Complex()
{
    real = 0;
    img = 0;
}
Complex::Complex(double re, double im)
{
    real = re;
    img = im;
}
void Complex::display()
{
    cout << real << "," << img << endl;
}
/*Complex operator+(Complex& comp1, Complex& comp2)
{
    Complex result;
    double realFlag, imgFlag;
    realFlag = comp1.get_real() + comp2.get_real();
    result.set_real(realFlag);
    imgFlag = comp1.get_img() + comp2.get_img();
    result.set_img(imgFlag);
    return result;
}*/
void excerise1()
{
    Complex comp1(1, 2);
    Complex comp2(3, 4);
    // Complex comp3 = comp1 + comp2;
    // comp3.display();
}
/***************************************************
 * 习题2：定义一个Complex类，重载‘+’'-''*''/'运算符，使
 * 其能够用于复数的加法运算，重载为类的成员函数
 * *************************************************/
/*Complex Complex::operator+(Complex& comp)
{
    Complex result;
    result.real = real + comp.real;
    result.img = img + comp.img;
    return result;
}*/
Complex Complex::operator-(Complex& comp)
{
    Complex result;
    result.real = real - comp.real;
    result.img = img - comp.img;
    return result;
}
void excerise2()
{
    Complex comp1(1, 2);
    Complex comp2(3, 4);
    //Complex comp3 = comp1 + comp2;
    //comp3.display();
    Complex comp4 = comp1 - comp2;
    comp4.display();
}
/***************************************************
 * 习题3：定义一个Complex类，重载‘+’使 * 其能够用于复数的加法运算，
 * 参数可以是两个类对象也可以是一个整数和一个类对象
 * *************************************************/
Complex operator+(Complex& comp1, Complex& comp2)
{
    Complex result;
    double realFlag, imgFlag;
    realFlag = comp1.get_real() + comp2.get_real();
    result.set_real(realFlag);
    imgFlag = comp1.get_img() + comp2.get_img();
    result.set_img(imgFlag);
    return result;
}
Complex operator+(int flag, Complex& comp)
{
    Complex result;
    result.real = comp.real + flag;
    result.img = comp.img;
    return result;
}
Complex operator+(Complex& comp, int flag)
{
    Complex result;
    result.real = comp.real + flag;
    result.img = comp.img;
    return result;
}
void excerise3()
{
    Complex comp1(1, 2);
    Complex comp2(3, 4);
    Complex comp3 = comp1 + comp2;
    comp3.display();
    Complex comp4 = comp1 + 4;
    comp4.display();
}
/***************************************************
 * 习题4：有两个矩阵a/b，均为2行3列，求二者之和，重载运算法‘+’
 * *************************************************/
rect::rect()
{
    memset(arry, 0, sizeof(arry));
}
rect::rect(int (&r)[2][3])
{
    memcpy(arry, r, sizeof(r));
}
rect rect::operator+(rect& re)
{
    rect result;
    for (int i = 0; i<2; i++) {
        for (int j = 0; j<3; j++) {
            result.arry[i][j] = arry[i][j] + re.arry[i][j];
        }
    }
    return result;
}
void rect::show()
{
    for (int i = 0; i<2; i++) {
        for (int j = 0; j<3; j++) {
            cout << arry[i][j] << " ";
        }
        cout << endl;
    }
}
void excerise4()
{
    int arr1[2][3] = {{1, 2, 3}, {4, 5, 6}};
    rect re1(arr1);
    int arr2[2][3] = {{1, 0, 1}, {4, 0, 1}};
    rect re2(arr2);
    rect re3 = re1 + re2;
    re3.show();
}
/***************************************************
 * 习题5：在4的基础上重载输入输出运算符，使之用于输入输出
 * *************************************************/
ostream& operator <<(ostream& output, rect& re)
{
    for (int i = 0; i<2; i++) {
        for (int j = 0; j<3; j++) {
            cout << re.arry[i][j] << " ";
        }
        cout << endl;
    }
    return output;
}
istream& operator >>(istream& input, rect& re)
{
    for (int i = 0; i<2; i++) {
        for (int j = 0; j<3; j++) {
            cin >> re.arry[i][j];
        }
    }
    return input;
}
void excerise5()
{
    rect re1, re2;
    cin >> re1;
    cin >> re2;
    rect re3 = re1 + re2;
    cout << re3;
}
int main()
{
    excerise5();
}